<template>
    <view class="goods-item">
      <!-- 左侧图片 -->
      <view class="goods-item-left">
        <!-- v-if控制勾选框是否显示 -->
        <radio :checked="goods.goods_state" color="#2b4b6b" v-if="showRadio" @click="radioChangeHandler"></radio>
        <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
      </view>
      <!-- 右侧信息 -->
      <view class="goods-item-right">
        <view class="goods-name">{{goods.goods_name}}</view>
        <view class="goods-info-box">
          <view class="goods-price">
            <!--  | 管道符，把前面的属性作为参数传递给后面的方法 -->
            ￥{{goods.goods_price | tofixed}}
          </view>
          <!-- 商品数量 -->
          <!-- 用户输入小数会被转化为整数，用户输入非法字符会被替换为默认值 1 -->
          <uni-number-box :min="1" :value="goods.goods_count" @change="numChangeHandler" v-if="showNum"></uni-number-box>
        </view>
      </view>
    </view>
</template>

<script>
  export default {
    name:"my-goods",
       // 接受外界传递的信息
    props:{
      // 商品信息对象
        goods:{
          type: Object,
          default:{},
        },
        // 是否展示左侧的radio\
        // 没有指定showradio的值代表不显示
        showRadio:{
          type:Boolean,
          default:false
        },
        showNum:{
          type:Boolean,
          default:false
        }
      },
    data() {
      return {
      defaultPic: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.m5_T0lZpnsmUufSoRnX5hAAAAA?w=156&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7'
      };
    },
    filters:{
      tofixed(num){
        return Number(num).toFixed(2)
      }
    },
    methods:{
      radioChangeHandler(){
        // 通过this.$emit方法去绑定外界的radio-change方法
        this.$emit('radio-change',{
          goods_id: this.goods.goods_id,
          goods_state: !this.goods.goods_state
        })
      },
      numChangeHandler(val){
       this.$emit('num-change',{
         // 传id
         goods_id:this.goods.goods_id,
         // 最新数量
         goods_count: +val
       })
      }
    }
  }
</script>

<style lang="scss">
.goods-item{
  width:750rpx;
  box-sizing: border-box;
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;
  .goods-item-left{
    margin-right: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .goods-pic{
      width: 100px;
      height: 100px;
      display: block;
    }
  }
  .goods-item-right{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    .goods-name{
      font-size: 13px;
    }
    .goods-price {
      font-size: 16px;
      color: #c00000;
    }
    .goods-info-box{
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }
}
</style>